﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using FileServer.DataConnectors;
using System.Data;

namespace FileServer
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RadFileExplorer1.Configuration.ContentProviderTypeName = typeof(FileServer.DataConnectors.DBContentProvider).AssemblyQualifiedName;
            RadFileExplorer1.Configuration.ViewPaths = new string[] { "ROOT" };

            if (Page.User.Identity.IsAuthenticated)
            {
                RadFileExplorer1.Configuration.UploadPaths = new string[] { "ROOT" };
                RadFileExplorer1.Configuration.DeletePaths = new string[] { "ROOT" };

            }

            AddDateAndOwnerColumns();

        }

        private void AddGridColumn(string name, string uniqueName, bool sortable)
        {
            //remove first if grid has a column with that name
            RemoveGridColumn(uniqueName);
            // Add a new column with the specified name
            GridTemplateColumn gridTemplateColumn1 = new GridTemplateColumn();
            gridTemplateColumn1.HeaderText = name;
            if (sortable)
                gridTemplateColumn1.SortExpression = uniqueName;
            gridTemplateColumn1.UniqueName = uniqueName;
            gridTemplateColumn1.DataField = uniqueName;

            RadFileExplorer1.Grid.Columns.Add(gridTemplateColumn1);
        }

        private void RemoveGridColumn(string uniqueName)
        {
            if (!Object.Equals(RadFileExplorer1.Grid.Columns.FindByUniqueNameSafe(uniqueName), null))
            {
                RadFileExplorer1.Grid.Columns.Remove(RadFileExplorer1.Grid.Columns.FindByUniqueNameSafe(uniqueName));
            }
        }

        private void AddDateAndOwnerColumns()
        {
            AddGridColumn("Creation Date", "DateCreated", true);
            AddGridColumn("Owner Name", "OwnerName", false);
            AddGridColumn("Download Count", "DownloadCount", false);
            AddGridColumn("Last Download Date", "LastDownloadDate", false);


        }

        
        
    }
}
